home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0094_Creating a SYS file in PASCAL.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  3.8 KB  |  149 lines

  1. {
  2.  EH> I was wondering if there was some way that I could convert a Pascal
  3.  EH> exe to some sys file that the computer loads/runs when booting.
  4.  
  5. You can use this, the only problem is that the units are not initialized (the
  6. optional code before the last end. in a unit is not executed), and so system
  7. (WriteLn/ReadLn) and crt (WriteLn/ReadLn) don't work.
  8.  
  9. ===
  10. { DEVCLINE.PAS: Example of a device driver in TP, Arne de Bruijn, 19960302. }
  11. { Released to the Public Domain. }
  12. { This example shows the 'commandline' of the device driver }
  13. { (everything after DEVICE=), and removes itself from memory. }
  14. type
  15.  TReqHead=record                   { Structure passed to us by DOS }
  16.   ReqLen:byte;
  17.   SubUnit:byte;
  18.   Cmd:byte;
  19.   Status:word;
  20.   Reserved:array[0..7] of byte;
  21.   MediaDesc:byte;
  22.   Address:pointer;
  23.   case byte of
  24.    0:(DevLine:pointer; DriveName:byte);
  25.   255:
  26.    (Count:word; Sector:word);
  27.  end;
  28.  
  29. var
  30.  DevStack:array[0..4094] of byte;  { Own stack, DOS's isn't that big }
  31.  EndOfStack:byte;
  32.  ReqHead:^TReqHead;
  33.  
  34. procedure DevStrat; far; forward;
  35. procedure DevIntr; far; forward;
  36.  
  37. procedure Header; assembler;
  38. { The trick: put the device header as the very first procedure your source, }
  39. { so TP places it at the start of the .exe }
  40. asm
  41.  dd -1                 { Next device in chain (updated by MS-DOS) }
  42.  dw 0                  { Device attribute, now block device }
  43.  dw offset DevStrat    { Offset of strategy routine }
  44.  dw offset DevIntr     { Offset of interrupt routine }
  45.  db 0,0,0,0,0,0,0,0    { For block: 1 byte no of subunits, 7 bytes reserved }
  46. end;
  47.  
  48. procedure DevStrat; assembler;
  49. { Strategy routine, save ES:BX for later use }
  50. asm
  51.  push ax
  52.  push ds
  53.  mov ax,seg @Data
  54.  mov ds,ax
  55.  mov word ptr [ReqHead],bx
  56.  mov word ptr [ReqHead+2],es
  57.  pop ds
  58.  pop ax
  59. end;
  60.  
  61. procedure WriteStr(S:string); assembler;
  62. { Units not initalized, can't use some System procs (WriteLn, etc.) }
  63. asm
  64.  cld
  65.  mov bx,ds
  66.  lds si,S
  67.  lodsb
  68.  mov cl,al
  69.  xor ch,ch
  70.  jcxz @NoStr
  71. @PrtStr:
  72.  lodsb
  73.  mov ah,2
  74.  mov dl,al
  75.  int 21h
  76.  loop @PrtStr
  77. @NoStr:
  78.  mov ds,bx
  79. end;
  80.  
  81. procedure TPIntr;
  82. { Called by asm proc, ReqHead contains pointer to request header, }
  83. { Local stack in datasegment used (now 4k) }
  84. type
  85.  AByte=array[0..65534] of byte;
  86. var
  87.  S:string[50];
  88.  I,IntNo:byte;
  89. begin
  90.  if ReqHead^.Cmd=0 then            { Initialization? }
  91.   begin
  92.    S[0]:=#50;                      { Max len of string }
  93.    Move(ReqHead^.DevLine^,S[1],50);{ Copy from DOS buffer }
  94.    I:=pos(#10,S);                  { Search for #10 }
  95.    if I>0 then                     { Found? }
  96.     begin
  97.      byte(S[0]):=I-1;              { That's the len for now }
  98.      I:=pos(#13,S);                { Also a #13? }
  99.      if I>0 then byte(S[0]):=I-1;  { That must be the length }
  100.     end;
  101.    WriteStr('Cmdline:"'+S+'"'#13#10);  { Display 'command line' }
  102.    { Remove device driver from memory }
  103.    ReqHead^.MediaDesc:=0;          { Number of components }
  104.    ReqHead^.Address:=ptr(cseg,0);  { First free address }
  105.    ReqHead^.Status:=$100;          { Status OK }
  106.   end
  107.  else
  108.   ReqHead^.Status:=$9003;          { Status unknown cmd }
  109. end;
  110.  
  111. procedure DevIntr; assembler;
  112. asm
  113.  push ax
  114.  push bx
  115.  push cx
  116.  push dx
  117.  push si
  118.  push di
  119.  push ds
  120.  push es
  121.  mov ax,seg @Data
  122.  mov ds,ax
  123.  mov bx,ss
  124.  mov cx,sp
  125.  mov ss,ax                  { Set up local stack }
  126.  mov sp,offset EndOfStack+1
  127.  push bx
  128.  push cx
  129.  call TPIntr
  130.  pop cx                     { Restore old stack pointer }
  131.  pop bx
  132.  mov ss,bx
  133.  mov sp,cx
  134.  pop es
  135.  pop ds
  136.  pop di
  137.  pop si
  138.  pop dx
  139.  pop cx
  140.  pop bx
  141.  pop ax
  142. end;
  143.  
  144. begin
  145.  ReqHead:=@Header; {To include it in linking (smartlinker skips it otherwise)}
  146.  { This is executed when run from the commandline }
  147.  WriteStr('Must be loaded from CONFIG.SYS with DEVICE=DEVCLINE.EXE'#13#10);
  148. end.
  149.